package com.binance.binancedemo.fragment

import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.binance.binancedemo.R
import com.binance.binancedemo.adapter.CommonPagerAdapter
import com.binance.binancedemo.api.IBinanceApi
import com.binance.binancedemo.contance.Constance
import com.binance.binancedemo.utils.L
import com.guoxiaomei.http.RetrofitServiceManager
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.f_quotation_tab.*
import java.util.*

/**
 * Created by xiangchong on 2017/12/15.
 */
class QuotationTabFragment : BaseFragment(){

    lateinit var mPagerAdapter : CommonPagerAdapter
    var mFragments = ArrayList<Fragment>()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
            inflater.inflate(R.layout.f_quotation_tab, null)


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        tool_bar.setTitle(R.string.market)
        vp_quotation.offscreenPageLimit = 4
        mPagerAdapter = CommonPagerAdapter(childFragmentManager, createFragments())
        tb_quotation.setupWithViewPager(vp_quotation)
        vp_quotation.adapter = mPagerAdapter
    }

    private fun createFragments(): MutableList<Fragment> {

        var bnbFragment = QuotationFragment()
        bnbFragment.arguments = Bundle()
        bnbFragment.arguments.putString(QuotationFragment.TYPE, Constance.BNB)
        mFragments.add(bnbFragment)

        var btcFragment = QuotationFragment()
        btcFragment.arguments = Bundle()
        btcFragment.arguments.putString(QuotationFragment.TYPE,  Constance.BTC)
        mFragments.add(btcFragment)

        var ethFragment = QuotationFragment()
        ethFragment.arguments = Bundle()
        ethFragment.arguments.putString(QuotationFragment.TYPE, Constance.ETH)
        mFragments.add(ethFragment)

        var usdtFragment = QuotationFragment()
        usdtFragment.arguments = Bundle()
        usdtFragment.arguments.putString(QuotationFragment.TYPE, Constance.USDT)
        mFragments.add(usdtFragment)
        return mFragments
    }

    override fun onFirstFrameVisible() {
        srl_refresh.setOnRefreshListener { onRefresh() }
        srl_refresh.isRefreshing = true
        onRefresh()
    }

    private fun onRefresh() {
        loadData()
    }

    private fun loadData() {
        var disposible = RetrofitServiceManager.create(IBinanceApi::class.java).getTicker24Hr()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnTerminate {
                    srl_refresh.isRefreshing = false
                }
                .subscribe({ tickers->
                    mFragments.forEach { (it as QuotationFragment).refreshCells(tickers) }
                }, {
                    L.e(it.toString())
                })
        addDisposable(disposible)

    }

}